package com.ui.basic.controller.administrative;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.common.basic.base.BaseController;
import com.common.basic.util.Globals;
import com.ui.basic.command.administrative.CommandExchangeRate;
import com.ui.basic.ibatis.model.system.TblSUser;
import com.ui.basic.service.administrative.ExchangeRateService;

@Controller
public class ExchangeRateController extends BaseController {
	private static final transient org.apache.commons.logging.Log LOGGER = org.apache.commons.logging.LogFactory.getLog(ExchangeRateController.class);
	
	@Autowired
	private ExchangeRateService exchangeRateService;

	public void setExchangeRateService(ExchangeRateService exchangeRateService) {
		this.exchangeRateService = exchangeRateService;
	}
	
	@RequestMapping(value = "/administrative/exchangeRate.page", method = RequestMethod.GET)
	public String page(
			final HttpServletRequest request,
			final HttpServletResponse response) {
		LOGGER.debug(">>>ExchangeRateController.page()");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		return "exchangeRate.page";
	}

	@RequestMapping(value = "/administrative/exchangeRate.view", method = RequestMethod.POST)
	public String view(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandExchangeRate") CommandExchangeRate commandExchangeRate) {
		LOGGER.debug(">>>ExchangeRateController.view");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			exchangeRateService.setCombo(commandExchangeRate);
			model.addAttribute("commandExchangeRate", exchangeRateService.getExchangeRate(commandExchangeRate));
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}
		return "exchangeRate.view";
	}

	@RequestMapping(value = "/administrative/exchangeRate.edit", method = RequestMethod.POST)
	public String edit(Model model, HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandExchangeRate") CommandExchangeRate commandExchangeRate) {
		LOGGER.debug(">>>ExchangeRateController.edit");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		try {
			exchangeRateService.setCombo(commandExchangeRate);
			if (commandExchangeRate.getId() != null)
				model.addAttribute("commandExchangeRate", exchangeRateService
						.getExchangeRateDetail(commandExchangeRate));
			else {
				TblSUser user = (TblSUser) request.getAttribute(Globals.USER_KEY);
				sendError(response, getMessage(request, "error.permissionDenied", new String[] { user.getUsername() }));
				return null;
			}
		} catch (SQLException e) {
			LOGGER.error(">>>Error :: " + e.getMessage());
			sendError(response, e.getMessage());
			return null;
		}

		return "exchangeRate.edit";
	}

	@RequestMapping(value = "/administrative/exchangeRate.save", method = RequestMethod.POST)
	public String save(HttpServletRequest request,
			HttpServletResponse response,
			@ModelAttribute("commandExchangeRate") CommandExchangeRate commandExchangeRate,
			BindingResult result, Model model) {
		LOGGER.debug(">>>ExchangeRateController.save");

		//** security validation
		if (getRequestValidation(request, response)==null) {
			return null;
		}

		validator.validate(commandExchangeRate, result);

		if (!hasFieldErrors(request, response, result)) {
			try {
				exchangeRateService.save(commandExchangeRate);
				return view(model, request, response, commandExchangeRate);
			} catch (Exception e) {
				if (commandExchangeRate.getId() != null)
					sendError(response, getMessage(request,
							"error.exchangeRatedata.saveFailed",
							new String[] { ""+commandExchangeRate.getId()}));
				else
					sendError(response, getMessage(request,
							"error.exchangeRatedata.exist", new String[] { ""+commandExchangeRate.getId() }));
				return null;
			}
		}
		return null;
	}
}
